//
//  DetailChartViewTool.swift
//  CheckmeRing
//
//  Created by 董鹏 on 2022/2/20.
//

import UIKit

class DetailChartViewTool: UIView {

    class func initDetailChartView(data: [RModel], isSpO2: Bool, sharePDF: Bool = false) -> LineChartView {
        
        let chartView = LineChartView()
        
        chartView.legend.form = .none // 图例
        chartView.legend.textColor = .clear
        chartView.xAxis.labelPosition = .bottom //x轴显示在下方
        chartView.xAxis.axisLineColor = hexColor("F3F3F3")! //x轴颜色
        chartView.xAxis.axisLineWidth = 0.8 // 线宽
        chartView.leftAxis.gridColor = hexColor("F3F3F3")!
        chartView.leftAxis.gridLineWidth = 1 // 线宽
        chartView.xAxis.drawGridLinesEnabled = false //不绘制网格线
        chartView.rightAxis.enabled = false //禁用右侧的Y轴
        chartView.leftAxis.drawAxisLineEnabled = false //不显示左侧Y轴
        chartView.scaleYEnabled = false //取消Y轴缩放
        chartView.doubleTapToZoomEnabled = false //双击缩放
        chartView.setVisibleXRangeMinimum(5)
        chartView.xAxis.spaceMax = 0.2 //最后一条数据距最左端距离，相当于偏移几个单位量。默认0.5
        
        //第一组散点图的10条随机数据
//        let dataEntries1 = (0..<data.count).map { (i) -> ChartDataEntry in
//            let val = Double(arc4random_uniform(100) + 3)
//            return ChartDataEntry(x: Double(i), y: val)
//        }
        var dataEntries1 = [ChartDataEntry]()
        var xValus = [String]()
        for (index, obj) in data.enumerated() {
            dataEntries1.append(ChartDataEntry.init(x: Double(index), y: isSpO2 ? Double(obj.spo2) : Double(obj.hr)))
            xValus.append(formatTimeStamp(time: obj.timeStamp, format: "HH:mm:ss"))
        }
        chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xValus)
        
        let chartDataSet = LineChartDataSet(entries: dataEntries1)
        chartDataSet.colors = [hexColor("3468E2")!] // 线条颜色
        chartDataSet.drawValuesEnabled = false //不绘制拐点上的文字
        chartDataSet.drawVerticalHighlightIndicatorEnabled = false // 不显示竖向十字线
        chartDataSet.drawHorizontalHighlightIndicatorEnabled = false //不显示横向十字线
        chartDataSet.drawCirclesEnabled = false //不绘制转折点
        chartDataSet.mode = .cubicBezier  // 折线样式
        
        if isSpO2 == true {
            // 血氧
            chartView.leftAxis.axisMinimum = 80 //最小刻度值
            chartView.leftAxis.axisMaximum = 100 //最大刻度值
            chartView.leftAxis.granularity = 5 //最小间隔
        }else {
            // 脉搏
            chartView.leftAxis.axisMinimum = 30 //最小刻度值
            chartView.leftAxis.axisMaximum = 150 //最大刻度值
            chartView.leftAxis.granularity = 30 //最小间隔
            // 是分享页面的
            if sharePDF == true {
                chartDataSet.drawFilledEnabled = true //开启填充色绘制
                chartDataSet.fillColor = hexColor("5482F1")!  //设置填充色
                chartDataSet.fillAlpha = 0.5 //设置填充色透明度
            }
        }
        
        let chartData = LineChartData(dataSet: chartDataSet)
        chartView.data = chartData
        
        return chartView
        
    }
    
    class func initMotionChartView(data: [RModel]) -> LineChartView {
        let chartView = LineChartView()
        
        chartView.legend.form = .none // 图例
        chartView.legend.textColor = .clear
        chartView.xAxis.labelPosition = .bottom //x轴显示在下方
        chartView.xAxis.axisLineColor = hexColor("F3F3F3")! //x轴颜色
        chartView.xAxis.axisLineWidth = 0.8 // 线宽
        chartView.leftAxis.gridColor = hexColor("F3F3F3")!
        chartView.leftAxis.gridLineWidth = 1 // 线宽
        chartView.xAxis.drawGridLinesEnabled = false //不绘制网格线
        chartView.rightAxis.enabled = false //禁用右侧的Y轴
        chartView.leftAxis.drawAxisLineEnabled = false //不显示左侧Y轴
//        chartView.leftAxis.axisMinimum = 0 //最小刻度值
//        chartView.leftAxis.axisMaximum = 8000 //最大刻度值
//        chartView.leftAxis.granularity = 2000 //最小间隔
        chartView.scaleYEnabled = false //取消Y轴缩放
        chartView.doubleTapToZoomEnabled = false //双击缩放
        chartView.xAxis.spaceMax = 0.2 //最后一条数据距最左端距离，相当于偏移几个单位量。默认0.5
        
        //第一组散点图的10条随机数据
//        let dataEntries1 = (0..<10).map { (i) -> ChartDataEntry in
//            let val = Double(arc4random_uniform(100) + 3)
//            return ChartDataEntry(x: Double(i), y: val)
//        }
        
        var dataEntries1 = [ChartDataEntry]()
        var xValus = [String]()
        for (index, obj) in data.enumerated() {
            dataEntries1.append(ChartDataEntry.init(x: Double(index), y: Double(obj.vector)))
            xValus.append(formatTimeStamp(time: obj.timeStamp, format: "HH:mm:ss"))
        }
        chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xValus)
        let chartDataSet = LineChartDataSet(entries: dataEntries1)
        chartDataSet.colors = [hexColor("3468E2")!]
        chartDataSet.drawValuesEnabled = false //不绘制拐点上的文字
        chartDataSet.drawVerticalHighlightIndicatorEnabled = false // 不显示竖向十字线
        chartDataSet.drawHorizontalHighlightIndicatorEnabled = false //不显示横向十字线
        chartDataSet.drawCirclesEnabled = false //不绘制转折点
        chartDataSet.drawFilledEnabled = true //开启填充色绘制
        chartDataSet.fillColor = hexColor("5482F1")!  //设置填充色
        chartDataSet.fillAlpha = 1 //设置填充色透明度
        
        let chartData = LineChartData(dataSet: chartDataSet)
        chartView.data = chartData
        
        return chartView
    }

    class func initRatioBarChartView(data: [RModel]) -> BarChartView {
        
        let chartView = BarChartView()
        
        chartView.legend.form = .none // 图例
        chartView.legend.textColor = .clear
        chartView.xAxis.labelPosition = .bottom //x轴显示在下方
        chartView.xAxis.axisLineColor = hexColor("F3F3F3")! //x轴颜色
        chartView.xAxis.axisLineWidth = 0.8 // 线宽
        chartView.leftAxis.gridColor = hexColor("F3F3F3")!
        chartView.leftAxis.gridLineWidth = 1 // 线宽
        chartView.xAxis.drawGridLinesEnabled = false //不绘制网格线
        chartView.rightAxis.enabled = false //禁用右侧的Y轴
        chartView.leftAxis.drawAxisLineEnabled = false //不显示左侧Y轴
        
        var dataEntries1 = [BarChartDataEntry]()
        
        // 95～100 95~90 85～90
        var maxNum = 0, mediumNum = 0
        for obj in data {
            if obj.spo2 > 95 {
                maxNum += 1
            }else if obj.spo2 > 90 && obj.spo2 <= 95 {
                mediumNum += 1
            }
        }
        
        let max = Float(maxNum) / Float(data.count)
        let medium = Float(mediumNum) / Float(data.count)
        let low = 1 - max - medium
        
        dataEntries1.append(BarChartDataEntry.init(x: Double(0), y: Double(max * 100.0)))
        dataEntries1.append(BarChartDataEntry.init(x: Double(1), y: Double(medium * 100.0)))
        dataEntries1.append(BarChartDataEntry.init(x: Double(2), y: Double(low * 100.0)))
        
        let xValus = ["100~95", "95~90", "90~85"]
        chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xValus)
        
        // 这20条数据作为柱状图的所有数据
        let chartDataSet = BarChartDataSet.init(entries: dataEntries1)
        // 目前柱状图只包括1组立柱
        let chartData = BarChartData(dataSets: [chartDataSet])
        // 设置柱子宽度为刻度区域的全部
        chartData.barWidth = 1
        // 立柱颜色
        chartDataSet.colors = [hexColor("5482F1")!]
        //设置柱状图数据
        chartView.data = chartData
        
        return chartView
    }
    
}
